﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Newtonsoft.Json;
using RN.ERP.Web;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using System.Configuration;

namespace RN.ERP.Web.Manage
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userGuid = txtGuid.Text.Trim();
            BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            RN.ERP.Web.ModelService.UserInfo userInfo = JsonConvert.DeserializeObject<RN.ERP.Web.ModelService.UserInfo>(client.DoLoginWithUGuid(userGuid, string.Empty, HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString()));
            if (userInfo.Result)
            {
                SessionUserInfo sui = new SessionUserInfo();
                sui.UserName = userInfo.UserName;
                sui.Name = userInfo.Name;
                sui.CorpUserGuid = userInfo.UserGuid;
                sui.DogId = userInfo.DogId;
                sui.RoleId = userInfo.RoleId;
                sui.FuncPerm = userInfo.FuncPerm;
                sui.AreaCode = userInfo.AreaCode;
                sui.LocalArea = userInfo.AreaCode2;
                sui.RoleType = userInfo.RoleType;
                sui.RoleTypeTitle = userInfo.RoleTypeTitle;
                sui.CurrentArea = string.IsNullOrEmpty(userInfo.AreaCode) ? string.Empty : userInfo.AreaCode.Substring(0, 2);
                string sysArea = ConfigurationManager.AppSettings["sysArea"];
                if (!string.IsNullOrEmpty(sysArea) && sysArea != sui.CurrentArea)
                {
                    sui.CurrentArea = sysArea;
                }
                HttpContext.Current.Session[SessionConstant.SESSION_USERINFO] = sui;
                ////当前市场范围，暂时处理,外地企业则需要首次登陆时确定市场范围
                ////用SessionUserInfo.CurrentArea替代。
                ////HttpContext.Current.Session[SessionConstant.SESSION_CURRENT_AREACODE] = userInfo.AreaCode;
                
                List<QueryCompanyInfoCondition> listCondition = new List<QueryCompanyInfoCondition>();
                QueryCompanyInfoCondition companyInfoQuery = new QueryCompanyInfoCondition();
                  Guid guid = Guid.Empty;               
                Guid.TryParse(userGuid, out guid);
                companyInfoQuery.CorpUserGuid = new Fly.Common.Linq.ParameterValue<Guid>() { O = OptEnum.Equal, V = guid };
                listCondition.Add(companyInfoQuery);
                EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
                var companyInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(listCondition), sui.CurrentArea));
                //TokenLogin(System.Web.HttpContext.Current,userInfo, companyInfo); 
                
                int appid = 0;
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings["AppID"], out appid);
                string areaCode = System.Configuration.ConfigurationManager.AppSettings["sysArea"];
                if ("06" == areaCode)
                {
                    enterpriseServiceClient.GenerateFlowCodebyAppId(userInfo.UserGuid, appid, areaCode);
                }
                Response.Redirect("../Mainframe.aspx");
            }
            else
            {
                string script = "$.ligerDialog.error('" + userInfo.Message + "');";
                ScriptManager.RegisterStartupScript(this, this.GetType(), string.Empty, script, true);
            }
        }


        //private void TokenLogin(HttpContext context, RN.ERP.Web.ModelService.UserInfo user, Tb_Corp_CompanyInfo corp)
        //{
        //    #region 令牌 edit by duan_yg
        //    //产生令牌
        //    string tokenValue = Guid.NewGuid().ToString().ToUpper();

        //    context.Response.Cookies.Add(new HttpCookie("LoginToken", tokenValue));
        //    HttpContext.Current.Session["token2"] = tokenValue;
        //    try
        //    {
        //        //WriteLog(string.Format("tokenValue:{0},Name:{1}", tokenValue, model.OldUser.Name));
        //        //产生主站凭证
        //        TokenService.TokenServiceSoapClient client = new TokenService.TokenServiceSoapClient();
        //        TokenService.DistriLogin userinfo = new TokenService.DistriLogin();
        //        userinfo.AreaCode = user.AreaCode;
        //        userinfo.RealAreaCode = "";
        //        userinfo.bGrade = string.IsNullOrEmpty(user.RoleId) ? -1 : int.Parse(user.RoleId);
        //        userinfo.ClassId = corp.classID;
        //        userinfo.CorpGuid = corp.CorpGuid;
        //        userinfo.isAdmin = user.RoleType != "User";
        //        userinfo.Name = user.Name;
        //        Guid userGuid = Guid.Empty;
        //        Guid.TryParse(user.UserGuid, out userGuid);
        //        userinfo.UserGuid = userGuid;
        //        userinfo.UserName = user.UserName;
        //        userinfo.DogId = string.IsNullOrEmpty(user.DogId) ? -1 : int.Parse(user.DogId);
        //        client.TokenInsert(tokenValue, userinfo);
        //    }
        //    catch (Exception ex)
        //    {
                
        //    }

        //    #endregion
        //}

    }
}